package com.example.ebeanbase.service;

import java.util.List;

import com.example.ebeanbase.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.ebean.EbeanServer;

@Service
public class UserService {

    @Autowired
    private EbeanServer ebeanServer;

    public List<User> getAll() {
        return ebeanServer.find(User.class).findList();
    }

    public User getById(Integer id) {
        return ebeanServer.find(User.class).where().eq("id", id).findOne();
    }

    public User selectUserByToken(String token, String roleType){
        return ebeanServer.find(User.class).where().eq("token",token).and().eq("role_type",roleType).findOne();
    }

    public User selectUserByUP(String username,String password){
        return ebeanServer.find(User.class).where().eq("username",username).and().eq("password",password).findOne();
    }

    public boolean updateUser(User user){
        int row = ebeanServer.find(User.class).where().eq("id", user.getId()).update();
        if(row<=0){
            return false;
        }
        return true;
    }

    public User loginSuccess(User user){
        user.update();
        return user;
    }
}